#include "dahengcameratask.h"
#include <QImage>
#include <QDateTime>
#include <QFile>
#include <QMutex>

DahengCameraTask::DahengCameraTask() {

}

void writeBmpFile(char *data, int w, int h,int imageNum)
{
    BITMAPFILEHEADER bitmapfileheader;  //bmp 头文件  14个字节
    bitmapfileheader.bfType = 0x4D42;//表示文件类型，BMP格式的文件这两个字节是0x4D42，10进制就是19778，字符显示就是‘BM’；
    bitmapfileheader.bfReserved1 = 0;//保留位必须设置为0
    bitmapfileheader.bfReserved2 = 0;//保留位必须设置为0
    bitmapfileheader.bfOffBits = 14 + 40;   //4字节的偏移

    BITMAPINFO  bitmapinfo;
    ZeroMemory(&bitmapinfo, sizeof(bitmapinfo));

    bitmapinfo.bmiHeader.biSize = sizeof(bitmapinfo.bmiHeader);  //40byte
    bitmapinfo.bmiHeader.biWidth = w;
    bitmapinfo.bmiHeader.biHeight = h;
    bitmapinfo.bmiHeader.biPlanes = 1;//颜色平面数 一般设置为1
    bitmapinfo.bmiHeader.biBitCount = 24;//每个像素占的bit数  1，2,4,8,16,24,32
    bitmapinfo.bmiHeader.biCompression = BI_RGB;  //压缩类型 常用是0表示不压缩
    bitmapinfo.bmiHeader.biSizeImage = (w * bitmapinfo.bmiHeader.biBitCount + 31) / 32 * 4 * h;//位图的大小
    bitmapinfo.bmiHeader.biClrUsed = 0;//2的biBitCount次方
    bitmapinfo.bmiHeader.biClrImportant = 0;
    bitmapinfo.bmiHeader.biXPelsPerMeter = 0;
    bitmapinfo.bmiHeader.biYPelsPerMeter = 0;

    QString pathname = "d:\\test\\seat\\";
    QString filename = "seatface";

    QFile hfile(pathname + filename + QString::number(imageNum) + ".bmp");
    if (hfile.open(QFile::WriteOnly))
    {
        char* bh = reinterpret_cast<char*>(&bitmapfileheader);
        hfile.write(bh, sizeof(bitmapfileheader));

        char *info = reinterpret_cast<char*>(&bitmapinfo);
        hfile.write(info, sizeof(bitmapinfo.bmiHeader));

        hfile.write(data, bitmapinfo.bmiHeader.biSizeImage);
    }
    hfile.close();
}


void DahengCameraTask::run(){
    int imageNum                = parameters["nImageNum"].toInt();
    GX_DEV_HANDLE hDevice       = (GX_DEV_HANDLE)(parameters["hDevice"].value<void*>());
    GX_FRAME_DATA* pFrameData   = (GX_FRAME_DATA*)(parameters["pFrameData"].value<void*>());
    char* pOutBuffer            = (char*)(parameters["pOutBuffer"].value<void*>());

    int m_nEditTimeOut = 500;
    //每次发送触发命令之前清空采集输出队列
    //防止库内部缓存帧，造成本次GXGetImage得到的图像是上次发送触发得到的图
    GXFlushQueue(hDevice);

    //发送软触发命令
    GXSetCommandValue(hDevice,"TriggerSoftware");

    qint64 CurrentMEpoch1 = QDateTime::currentMSecsSinceEpoch();
    //获取图像
    GXGetImage(hDevice, pFrameData, m_nEditTimeOut);

    int nWidth = 5496;
    int nHeight = 3672;

    //char* pOutBuffer = MainWindow::instance()->GetImageSrcBuffer(imageNum);

    //char* pOutBuffer = (char*) malloc(nWidth * nHeight * 3 * sizeof(char));

    DxRaw8toRGB24(pFrameData->pImgBuf,
                  pOutBuffer,
                  nWidth,
                  nHeight,
                  DX_BAYER_CONVERT_TYPE::RAW2RGB_ADAPTIVE,
                  DX_PIXEL_COLOR_FILTER::BAYERRG,
                  true);

    writeBmpFile(pOutBuffer,nWidth,nHeight,imageNum);

    qint64 CurrentMEpoch2 = QDateTime::currentMSecsSinceEpoch();
    qDebug() << "采集一张图片，耗时：" << QString::number(CurrentMEpoch2 - CurrentMEpoch1) << "毫秒";

}
